#pragma once

#include <iostream>
#include <vector>
#include <string>
#include <ctemplate/template.h>

#include "../comm/log.hpp"
// #include "oj_model.hpp"
#include "oj_model2.hpp"

namespace ns_view
{
    using namespace std;
    using namespace ns_model;

    const string template_path = "./template_html/";

    class View
    {
    public:
        View() {}
        ~View() {}
        void AllExpandHtml(const vector<struct Question> &questions, string *html)
        {
            // 形成路径
            string src_html_path = template_path + "all_questions.html";

            // 形成数据字典
            ctemplate::TemplateDictionary root("all questions");
            for (const auto &q : questions)
            {
                // 获取一个子字典，子字典内添加一个题目的数据
                ctemplate::TemplateDictionary *sub = root.AddSectionDictionary("question_list");
                sub->SetValue("number", q.number);
                sub->SetValue("title", q.name);
                sub->SetValue("star", q.star);
            }

            // 指定被渲染的html
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_html_path, ctemplate::DO_NOT_STRIP);

            // 执行渲染
            tpl->Expand(html, &root);
        }
        void OneExpandHtml(const struct Question &q, string *html)
        {
            // 形成路径
            string src_html_path = template_path + "one_question.html";
            // 形成数据字典
            ctemplate::TemplateDictionary root("one question");
            root.SetValue("number", q.number);
            root.SetValue("title", q.name);
            root.SetValue("star", q.star);
            root.SetValue("desc", q.desc); 
            root.SetValue("pre_code", q.header);
            // 获取被渲染的网页
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_html_path, ctemplate::DO_NOT_STRIP);
            // 执行渲染
            tpl->Expand(html, &root);
        }
    };  

} // namespace ns_view
